package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.dao.impl.ProductImpl;
import com.cn.wanxi.model.PageBean;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.util.JDBC;
import redis.clients.jedis.Jedis;

public class ProductServiceImpl implements ProductService {
    @Override
    public PageBean findAll(ProductModel productModel) {
        ProductDao productDao = new ProductImpl();
        PageBean<ProductModel> pageBean = new PageBean<>();
        pageBean.setCount(productDao.getCount(productModel));
        pageBean.setList(productDao.findAll(productModel));
        return pageBean;
    }

    @Override
    public String add(ProductModel productModel) {
        ProductDao productDao = new ProductImpl();

        return productDao.add(productModel) > 0 ? "success" : "error";
    }

    @Override
    public ProductModel findById(int id) {
        ProductDao productDao = new ProductImpl();
        Jedis jedis = new Jedis();
//        点击次数
        jedis.incr("product_id_" + id + "_count");
        return productDao.findById(id);
    }

    @Override
    public String update(ProductModel productModel) {
        ProductDao productDao = new ProductImpl();

        return productDao.update(productModel) > 0 ? "success" : "error";
    }
}
